<?php

/*
 +-----------------------------------------------------------------------+
 | plugins/amavisdnewsql/amavisdnewsql_sync_whitelist.inc                    |
 |                                                                       |
 | This file is part of the RoundCube Webmail client                     |
 | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland                 |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | PURPOSE:                                                              |
 |   Synchronise address book and whitelist addresses in SpamAssassin    |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: Philip Weir                                                   |
 +-----------------------------------------------------------------------+
 | Requires: plugins/amavisdnewsql/config.inc.php                          |
 +-----------------------------------------------------------------------+

 $Id: $

*/

function amavisdnewsql_sync_whitelist($uid, $a_record, $cid, $action) {
	include('plugins/amavisdnewsql/config.inc.php');

	// get username
	$user = new rcube_user($uid, false);
	$un = $user->data['username'];

	if ($amavisdnewsql_config['whitelist_sync'] && !empty($un)) {
		$user_db = new rcube_mdb2($amavisdnewsql_config['db_dsnw'], $amavisdnewsql_config['db_dsnr'], $amavisdnewsql_config['db_persistent']);
		$user_db->db_connect('w');

		$email = $a_record['email'];
		switch ($action) {
			case 'insert':
				// check address is not already whitelisted
				$sql_result = $user_db->query("SELECT value FROM ". $amavisdnewsql_config['sql_table_name'] ." WHERE ". $amavisdnewsql_config['sql_username_field'] ." = '". $un ."' AND ". $amavisdnewsql_config['sql_preference_field'] ." = 'whitelist_from' AND ". $amavisdnewsql_config['sql_value_field'] ." = '". $email ."';");

				if ($user_db->num_rows($sql_result) == 0)
					$user_db->query("INSERT INTO ". $amavisdnewsql_config['sql_table_name'] ." (". $amavisdnewsql_config['sql_username_field'] .", ". $amavisdnewsql_config['sql_preference_field'] .", ". $amavisdnewsql_config['sql_value_field'] .", ". $amavisdnewsql_config['sql_contactid_field'] .") VALUES ('". $un ."', 'whitelist_from', '". $email ."', ". $cid .");");
				break;
			case 'update':
				// check address is not already whitelisted
				$sql_result = $user_db->query("SELECT value FROM ". $amavisdnewsql_config['sql_table_name'] ." WHERE ". $amavisdnewsql_config['sql_username_field'] ." = '". $un ."' AND ". $amavisdnewsql_config['sql_preference_field'] ." = 'whitelist_from' AND ". $amavisdnewsql_config['sql_value_field'] ." = '". $email ."';");

				if ($user_db->num_rows($sql_result) == 0)
					$user_db->query("UPDATE ". $amavisdnewsql_config['sql_table_name'] ." SET ". $amavisdnewsql_config['sql_value_field'] ." = '". $email ."' WHERE ". $amavisdnewsql_config['sql_username_field'] ." = '". $un ."' AND ". $amavisdnewsql_config['sql_preference_field'] ." = 'whitelist_from' AND ". $amavisdnewsql_config['sql_contactid_field'] ." = ". $cid .";");
				break;
			case 'delete':
				$user_db->query("DELETE FROM ". $amavisdnewsql_config['sql_table_name'] ." WHERE ". $amavisdnewsql_config['sql_username_field'] ." = '". $un ."' AND ". $amavisdnewsql_config['sql_preference_field'] ." = 'whitelist_from' AND ". $amavisdnewsql_config['sql_contactid_field'] ." IN (". $cid .");");
				break;
			case 'deleteall':
				$user_db->query("DELETE FROM ". $amavisdnewsql_config['sql_table_name'] ." WHERE ". $amavisdnewsql_config['sql_username_field'] ." = '". $un ."' AND ". $amavisdnewsql_config['sql_preference_field'] ." = 'whitelist_from' AND ". $amavisdnewsql_config['sql_contactid_field'] ." > -1;");
				break;
		}
	}
}

?>